home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / util / misc / VMM_src.lha / VMM / debug_tools / print_debug.c
Encoding:
C/C++ Source or Header  |  1995-05-17  |  3.5 KB  |  145 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <exec/execbase.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #ifndef __GNUC__
  8. #include <clib/exec_protos.h>
  9. #else
  10. #include <inline/exec.h>
  11. #endif
  12.  
  13. #define DEBUG
  14.  
  15. #ifdef __GNUC__
  16. #include "../shared_defs.h"
  17. #else
  18. #include "/shared_defs.h"
  19. #endif
  20.  
  21. static char rcsid [] = "$Id: print_debug.c,v 1.2 93/09/02 13:35:26 Martin_Apel Exp $";
  22.  
  23. #define FILBUF_SIZE (64L * 1024L)
  24. #define OFFSET_OF(struct_name, member) ((ULONG)&(((struct struct_name*)0)->member))
  25.  
  26. extern struct ExecBase *SysBase;
  27. struct DebugBuffer *DebugBuffer;
  28.  
  29. main ()
  30.  
  31. {
  32. char *FileBuffer;
  33. char *tmp;
  34. int length;
  35. FILE *debout;
  36. ULONG debug_start;
  37. struct MemHeader *mh;
  38. BOOL success = FALSE,
  39.      Allocated = FALSE;
  40.  
  41. Forbid ();
  42.  
  43. for (mh = (struct MemHeader*) SysBase->MemList.lh_TailPred;
  44.      !success && (mh->mh_Node.ln_Pred != NULL); 
  45.      mh = (struct MemHeader*) mh->mh_Node.ln_Pred)
  46.   {
  47.   debug_start = (ULONG) mh->mh_Upper - sizeof (struct DebugBuffer);
  48.   DebugBuffer = (struct DebugBuffer*) debug_start;
  49.   if ((DebugBuffer->Magic1 == DEBUG_MAGIC1) && 
  50.       (DebugBuffer->Magic2 == DEBUG_MAGIC2) &&
  51.       (DebugBuffer->ThisBuffer == DebugBuffer))
  52.     {
  53.     success = TRUE;
  54.     if (AllocAbs (sizeof (struct DebugBuffer), (APTR)debug_start) == NULL)
  55.       {
  56.       Permit ();
  57.       printf ("Found debug buffer, but could not allocate it\n");
  58.       printf ("Trying to read it anyway. Contents may be garbled\n");
  59.       }
  60.     else
  61.       Allocated = TRUE;
  62.     }
  63.   }
  64.  
  65. Permit ();
  66.  
  67. if (!success)
  68.   {
  69.   /* Try to find debug buffer somewhere in memory */
  70.   ULONG *mem_ptr;
  71.  
  72.   printf ("Didn't find debug buffer at default location\nSearching memory...\n");
  73.  
  74.   for (mh = (struct MemHeader*)SysBase->MemList.lh_Head; 
  75.        !success && mh->mh_Node.ln_Succ != NULL; 
  76.        mh = (struct MemHeader*)mh->mh_Node.ln_Succ)
  77.     {
  78.     for (mem_ptr = (ULONG*)mh->mh_Lower; 
  79.          mem_ptr < (ULONG*)mh->mh_Upper;
  80.          mem_ptr++)
  81.       {
  82.       if (*mem_ptr == DEBUG_MAGIC1)
  83.         {
  84.         /* Probably found it */
  85.         DebugBuffer = (struct DebugBuffer*) ((UBYTE*)mem_ptr - OFFSET_OF (DebugBuffer, Magic1));
  86.         if (DebugBuffer->ThisBuffer == DebugBuffer && DebugBuffer->Magic2 == DEBUG_MAGIC2)
  87.           {
  88.           success = TRUE;
  89.           if (AllocAbs (sizeof (struct DebugBuffer), (APTR)DebugBuffer) == NULL)
  90.             {
  91.             printf ("Found debug buffer, but could not allocate it\n");
  92.             printf ("Trying to read it anyway. Contents may be garbled\n");
  93.             }
  94.           else
  95.             Allocated = TRUE;
  96.           break;
  97.           }
  98.         }
  99.       }
  100.     }
  101.  
  102.   if (!success)
  103.     {
  104.     printf ("Didn't find valid debug buffer\nExiting...\n");
  105.     exit (10);
  106.     }
  107.   }
  108.  
  109. printf ("Found debug buffer\n");
  110.  
  111. if ((FileBuffer = AllocMem (FILBUF_SIZE, MEMF_PUBLIC)) == NULL)
  112.   {
  113.   printf ("couldn't allocate mem for file buffer\n");
  114.   if (Allocated)
  115.     FreeMem (DebugBuffer, sizeof (struct DebugBuffer));
  116.   exit (5);
  117.   }
  118.  
  119. if ((debout = fopen ("trace", "w")) == NULL)
  120.   {
  121.   printf ("Couldn't open trace file\n");
  122.   if (Allocated)
  123.     FreeMem (DebugBuffer, sizeof (struct DebugBuffer));
  124.   exit (5);
  125.   }
  126.  
  127. setvbuf (debout, FileBuffer, _IOFBF, FILBUF_SIZE);
  128.  
  129. for (tmp = DebugBuffer->DbgInfo; tmp < DebugBuffer->NextFree;)
  130.   {
  131.   length = strlen (tmp);
  132.   if (!(length & 1))
  133.     length++;
  134.   fprintf (debout, tmp, *((ULONG*)(tmp + length + 1)));
  135.   fputc ('\n', debout);
  136.   tmp += length + 5;
  137.   }
  138.  
  139. if (Allocated)
  140.   FreeMem (DebugBuffer, sizeof (struct DebugBuffer));
  141. fclose (debout);
  142. FreeMem (FileBuffer, FILBUF_SIZE);
  143. exit (0);
  144. }
  145.